package com.pelli.imarket.server;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RESTServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 416015537087035695L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		String url = req.getHeader("restURL");
		String remote = req.getHeader("remote");
		if(!remote.endsWith("/")){
			remote+="/";
		}
		URL _url = new URL(remote+url);
		URLConnection con = _url.openConnection();
		InputStream in = con.getInputStream();
		OutputStream out = resp.getOutputStream();
		byte[] buffer = new byte[1024];
		int ch=0;
		while((ch=in.read(buffer))>0){
			out.write(buffer, 0, ch);
		}
		in.close();
	}
	
	

}
